package com.example.hospital_208.controller;

import com.example.hospital_208.po.DoctorWork;
import com.example.hospital_208.service.DoctorWorkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/doctor-works")
@Tag(name = "医生排班管理接口", description = "医院医生排班信息的增删改查操作")
public class DoctorWorkController {

    private final DoctorWorkService doctorWorkService;

    public DoctorWorkController(DoctorWorkService doctorWorkService) {
        this.doctorWorkService = doctorWorkService;
    }

    // 获取所有医生排班记录
    @GetMapping
    @Operation(summary = "查询所有医生排班记录", description = "获取医院所有医生的排班信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = DoctorWork.class)))
    })
    public List<DoctorWork> getAllDoctorWorks() {
        return doctorWorkService.listDoctorWorks();
    }

    // 根据医生ID获取排班记录（新增）
    @GetMapping("/{doc_id}")
    @Operation(summary = "根据医生ID查询排班记录", description = "通过医生ID获取该医生的所有排班信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = DoctorWork.class))),
            @ApiResponse(responseCode = "404", description = "未找到该医生的排班记录")
    })
    public List<DoctorWork> getDoctorWorksByDocId(
            @PathVariable("doc_id")
            @Parameter(description = "医生ID", example = "D001", required = true)
            String docId) {
        return doctorWorkService.getDoctorWorksByDocId(docId);
    }

    // 根据日期获取排班记录（新增）
    @GetMapping("/date/{day}")
    @Operation(summary = "根据日期查询排班记录", description = "通过日期获取该日所有医生的排班信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = DoctorWork.class))),
            @ApiResponse(responseCode = "404", description = "未找到该日期的排班记录")
    })
    public List<DoctorWork> getDoctorWorksByDay(
            @PathVariable("day")
            @Parameter(description = "日期（格式：YYYY-MM-DD）", example = "2025-06-24", required = true)
            String day) {
        return doctorWorkService.getDoctorWorksByDay(day);
    }

    // 根据医生ID和日期获取排班详情（保留原接口）
    @GetMapping("/{doc_id}/{day}")
    @Operation(summary = "根据医生ID和日期查询排班", description = "通过医生ID和日期获取具体排班信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = DoctorWork.class))),
            @ApiResponse(responseCode = "404", description = "排班记录不存在")
    })
    public DoctorWork getDoctorWorkByDocIdAndDay(
            @PathVariable("doc_id")
            @Parameter(description = "医生ID", example = "D001", required = true)
            String docId,

            @PathVariable("day")
            @Parameter(description = "日期（格式：YYYY-MM-DD）", example = "2025-06-24", required = true)
            String day) {
        return doctorWorkService.getDoctorWorkByDocIdAndDay(docId, day);
    }

    // 新增医生排班记录
    @PostMapping
    @Operation(summary = "新增医生排班记录", description = "向系统中添加医生的排班信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "添加成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或排班已存在")
    })
    public String addDoctorWork(@RequestBody
                                @Parameter(description = "医生排班实体对象",
                                        content = @Content(schema = @Schema(implementation = DoctorWork.class)))
                                DoctorWork doctorWork) {
        int result = doctorWorkService.addDoctorWork(doctorWork);
        return result > 0 ? "医生排班记录添加成功" : "医生排班记录添加失败";
    }

    // 修改医生排班记录
    @PutMapping
    @Operation(summary = "修改医生排班记录", description = "更新已有医生的排班信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或排班不存在")
    })
    public String updateDoctorWork(@RequestBody
                                   @Parameter(description = "包含更新信息的医生排班实体",
                                           content = @Content(schema = @Schema(implementation = DoctorWork.class)))
                                   DoctorWork doctorWork) {
        int result = doctorWorkService.editDoctorWork(doctorWork);
        return result > 0 ? "医生排班记录修改成功" : "医生排班记录修改失败";
    }

    // 删除医生所有排班记录（仅根据医生ID）
    @DeleteMapping("/{doc_id}")
    @Operation(summary = "删除医生所有排班记录", description = "根据医生ID删除该医生的所有排班信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或删除失败")
    })
    public String deleteDoctorWorksByDocId(
            @PathVariable("doc_id")
            @Parameter(description = "待删除的医生ID", example = "D001", required = true)
            String docId) {
        int result = doctorWorkService.removeDoctorWorksByDocId(docId);
        return result > 0 ? "医生排班记录删除成功" : "医生排班记录删除失败";
    }
}